package com.sk.k01.m01.controller;

import com.sk.k01.m01.entity.User;
import com.sk.k01.m01.service.UserService;
import com.sk.k01.m01.utils.Msg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author L
 * @since 2020-07-27
 */
@Api(tags={"用户管理"})
@CrossOrigin
@RestController
@RequestMapping("/m01/user")
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation("查询所有用户")
    @GetMapping("findAll")
    public Msg findAll() {
        List<User> list = userService.selectList(null);
        return Msg.success().data("items",list);
    }

    @ApiOperation("根据ID查询用户")
    @GetMapping("{id}")
    public Msg findById(
            @ApiParam(name = "id",value = "用户ID",required = true)
            @PathVariable String id) {
        User user = userService.selectById(id);
        return Msg.success().data("user",user);
    }

    @ApiOperation("根据ID删除用户")
    @DeleteMapping("{id}")
    public Msg remove(
            @ApiParam(name = "id",value = "用户ID",required = true)
            @PathVariable String id) {
        Long result = userService.delete(id);
        if(result>0){
            return Msg.success();
        }else {
            return Msg.error();
        }
    }

    @ApiOperation("添加用户")
    @PostMapping("add")
    public Msg add(
            @RequestBody(required = false) User user) {
        Long result = userService.insert(user);
        if(result>0){
            return Msg.success();
        }else {
            return Msg.error();
        }
    }

    @ApiOperation("修改用户")
    @PostMapping("edit")
    public Msg edit(
            @RequestBody(required = false) User user) {
        Long result = userService.update(user);
        if(result>0){
            return Msg.success();
        }else {
            return Msg.error();
        }
    }
}

